package com.hetao.jmb2c.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hetao.jmb2c.common.PageResult;
import com.hetao.jmb2c.common.Result;
import com.hetao.jmb2c.entity.Category;
import com.hetao.jmb2c.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "产品分类管理", description = "产品分类")
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @Operation(summary = "新增分类",description = "新增分类,保留了sellerId方便将来扩展成b2b2c项目")
    @PostMapping("")
    public Result<Object> category(@RequestBody Category category) {
        this.categoryService.save(category);
        return Result.success();
    }

    @Operation(summary = "查询分类列表")
    @GetMapping("list")
    public Result<List<Category>> list(){
        List<Category> list = this.categoryService.list();
        return Result.success(list);
    }

    @Operation(summary = "按id查询分类")
    @GetMapping("{id}")
    public Result<Category> get(@PathVariable("id") Long id){
        Category category = this.categoryService.getById(id);
        return Result.success(category);
    }

    @Operation(summary = "分页查询分类")
    @GetMapping("page")
    public PageResult<Category> page(Long current, Long pageSize, String name){
        IPage<Category> page= this.categoryService.getPage(current,pageSize,name);
        return PageResult.success(page);
    }

    @Operation(summary = "修改分类")
    @PutMapping()
    public Result<Object> update(@RequestBody Category category){
        this.categoryService.updateById(category);
        return Result.success();
    }

    @Operation(summary = "批量删除分类")
    @DeleteMapping("")
    public Result<Object> delete(@RequestBody List<Long> ids){
        this.categoryService.removeBatchByIds(ids);
        return Result.success();
    }

}
